package cn.chen.hd.fs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.*;
import java.net.URI;
import java.util.List;

public class HdfsFileSystemTest {

    public static void main(String[] args) {
        Configuration conf = new Configuration();
//        System.out.println(HdfsFileSystemTest.testCheckFileExist("hdfs://localhost:9000/chen", conf));
        HdfsFileSystemTest.testLocationFile("hdfs://localhost:9000/test", conf);
    }

    public static void testLocationFile(String uri, Configuration conf){
        try {
            FileSystem fs = FileSystem.get(URI.create(uri), conf);
            FileStatus fileStatus = fs.getFileStatus(new Path(uri));
            BlockLocation[] blockLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
            int blockLen = blockLocations.length;
            for(int i = 0;i < blockLen;i ++){
                String[] host = blockLocations[i].getHosts();
                System.out.println("block_" + i + "_location:" + host[0]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void testFileList(String uri, Configuration conf){
        try {
            FileSystem fs = FileSystem.get(URI.create(uri), conf);
            FileStatus[] list = fs.listStatus(new Path(uri));
            for(FileStatus status : list){
                System.out.println(status.getPath().toString());
            }
            fs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean testCheckFileExist(String uri, Configuration conf){
        try {
            FileSystem fs = FileSystem.get(URI.create(uri), conf);
            return fs.exists(new Path(uri));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void testCopyFromLocal(String source, String destination, Configuration conf){
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(source));
            FileSystem fs = FileSystem.get(URI.create(destination), conf);
            OutputStream out = fs.create(new Path(destination));
            IOUtils.copyBytes(in, out, 4096, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean testDelete(String uri, Configuration conf){
        try {
            FileSystem fs = FileSystem.get(URI.create(uri), conf);
            return fs.delete(new Path(uri),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void testMkdir(String uri, Configuration conf){
        try {
            FileSystem fs = FileSystem.get(URI.create(uri), conf);
            fs.mkdirs(new Path(uri));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
